package com.newStart;

import java.lang.reflect.Method;

/**
 * Project: tobe-better-coder
 * Package: com.newStart
 *
 * @author : Mr.xu
 * @date : 2022/8/15 22:32
 */

interface Test {
    void method1();
}

interface InvokeHandler {
    Object invoke(Object object, Method method, Object... arg);
}

public class ProxyDemo {
    public static void main(String[] args) {
        testProxy(new InvokeHandler() {
            @Override
            public Object invoke(Object object, Method method, Object... arg) {
                System.out.println("代理Test的method1方法");
                return null;
            }
        }, Test.class);
    }

    public static Test testProxy(final InvokeHandler invokeHandler, final Class<?> clazz) {
        return new Test() {
            @Override
            public void method1() {
                try {
                    Method method1 = clazz.getMethod("method1");
                    invokeHandler.invoke(clazz, method1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }
}